﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Caliburn.PresentationFramework.ApplicationModel;
using Caliburn.Core.Metadata;
using XWord.WPF.ApplicationFramework;
using Caliburn.PresentationFramework.Actions;
using XWord.Fundamental.Domain;
using System.IO;
using Microsoft.Win32;
using System.Collections;
using XWord.GUI.Presenters.Interface;
using XWord.GUI.Presenters.Words;
using Microsoft.Practices.ServiceLocation;
using XWord.GUI.Presenters.Adorns;
using System.ComponentModel;
using System.Threading;
using XWord.Fundamental.Domain.Services;
namespace XWord.GUI.Presenters.Fundamental
{
    [Singleton("FootStatus", typeof(FootStatusPresenter))]
    public class FootStatusPresenter : Presenter
    {
        public FootStatusPresenter(IWordExplorerPresenter wordExplorerPresenter)
        {
            this.WordExplorerPresenter = wordExplorerPresenter as WordExplorerPresenter;
            
        }

        protected override void OnInitialize()
        {
            Statisic();
            //WordBehavior.WordEventHandler += new WordEventHandler(WordBehavior_WordEventHandler);
            base.OnInitialize();
        }

        void WordBehavior_WordEventHandler(object sender, WordEventArgs args)
        {
            switch (args.Action)
            {
                case WordAction.Add:
                    Statisic();
                    break;
                case WordAction.Remove:
                    Statisic();
                    break;
            }
        }

        private void Statisic()
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback((object obj) =>
            {
                var counts = _filter.Statisic();
                //var counts = RemotingServiceFactory.GetWordService().GetWordStatisic();
                AllWordListCount = counts[0];
                NotFamilarCount = counts[1];
                NotEtymaCount = counts[2];
            }));
        }

        private WordFilter _filter = new WordFilter();

        protected WordExplorerPresenter WordExplorerPresenter { get; set; }

        public void DisplayNotFamilarWordList()
        {
           NotFamilarCount = _filter.FilerNoFamilarWordList();
        }

        public IWordBehavior WordBehavior { get; set; }

        public void DisplayNotEtymaWordList()
        {
            NotEtymaCount = _filter.FilerNoEtymaWordList();
        }
        //public void DisplayTodayWordList()
        //{
            
        //    WordExplorerPresenter.WordList = WordExplorerPresenter.AllWordList.Where(e => CheckWordUpdateTime(e.UpdateTime)).ToObservableCollection();
        //}

        public IList<string> AllWordList
        {
            get
            {
                return WordExplorerPresenter.AllWordList;
            }
        }

        private bool CheckWordUpdateTime(DateTime? time)
        {
            if (!time.HasValue) return false;
            var value = time.Value;
            var now = DateTime.Now;
            return value.Year == now.Year && value.Month == now.Month && value.Day == now.Day;
        }

        #region Property

        private int _allWordListCount;
        public int AllWordListCount
        {
            get { return _allWordListCount; }
            set
            {
                _allWordListCount = value;
                NotifyOfPropertyChange("AllWordListCount");
            }
        }

        private int _notFamilarCount;
        public int NotFamilarCount
        {
            get { return _notFamilarCount; }
            set
            {
                _notFamilarCount = value;
                NotifyOfPropertyChange("NotFamilarCount");
            }
        }

        private int _notEtymaCount;
        public int NotEtymaCount
        {
            get { return _notEtymaCount; }
            set
            {
                _notEtymaCount = value;
                NotifyOfPropertyChange("NotEtymaCount");
            }
        }

        #endregion

        [AsyncAction]
        public void ImportWordList()
        {
            //WordImport import = new WordImport();
            //import.Excute(
            //    (Word word) => { return !WordExplorerPresenter.AllWordList.Any(ee => ee.Name.Trim() == word.Name); },
            //    ((Word word) =>
            //    {
            //        //_wordModel.SaveWord(word);
            //        WordExplorerPresenter.AllWordList.Add(word);
            //    }));
        }

        public void ExportTodayWordList()
        {
            //var list = WordExplorerPresenter.AllWordList.Where(e => CheckWordUpdateTime(e.UpdateTime)).ToList();
            //Newtonsoft.Json.JsonSerializer js = new Newtonsoft.Json.JsonSerializer();
            //var a = new StringWriter();
            //js.Serialize(a, list);
            //SaveFileDialog dialog = new SaveFileDialog();
            //dialog.DefaultExt = "txt";
            //dialog.AddExtension = true;
            //var result = dialog.ShowDialog();
            //if (result.Value)
            //{
            //    using (StreamWriter sw = File.CreateText(dialog.FileName))
            //    {
            //        sw.WriteLine(a.ToString());
            //    }
            //}
            //a.Close();
        }

        public void ImportTodayWordList()
        {
            //OpenFileDialog dialog = new OpenFileDialog();
            //var result = dialog.ShowDialog();
            //if (result.Value)
            //{

            //    using (TextReader streamReader =
            //       new StreamReader(dialog.FileName, Encoding.UTF8))
            //    {
            //        Newtonsoft.Json.JsonSerializer js = new Newtonsoft.Json.JsonSerializer();
            //        var list = js.Deserialize(new StringReader(streamReader.ReadToEnd()), typeof(IList)) as IList;
            //        foreach (Newtonsoft.Json.Linq.JObject item in list)
            //        {
            //            var jvalue = item["Name"] as Newtonsoft.Json.Linq.JValue;

            //            var word = AllWordList.Where(e => e.Name == jvalue.Value.ToString());
            //            if (word.Count() > 0)
            //            {
            //                var currentWord = word.First();
            //                currentWord.Suffix = (item["Suffix"] as Newtonsoft.Json.Linq.JValue).Value.ToString();
            //                currentWord.IsIgnore = true;
            //             //   _wordModel.SaveWord(currentWord);
            //            }
            //            else
            //            {
            //                WordImport import = new WordImport();
            //                import.ImportWord(jvalue.Value.ToString(),
            //                    (Word word1) => { return !AllWordList.Any(ee => ee.Name.Trim() == word1.Name); },
            //                    ((Word word1) =>
            //                    {
            //                        word1.CreateTime = DateTime.Now;
            //                     //   _wordModel.SaveWord(word1);
            //                        AllWordList.Add(word1);
            //                        //SearchWord(SearchText);
            //                    }));
            //            }
            //        }
            //    }
            //}
        }
    }
}
